package com.example.beautyuser.service.Impl;

import com.example.beautyuser.dto.LoginRequest;
import com.example.beautyuser.dto.LoginResponse;
import com.example.beautyuser.entity.User;
import com.example.beautyuser.repostiry.UserRepository;
import com.example.beautyuser.service.UserService;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    private PasswordEncoder passwordEncoder;
//    private JwtTokenProvider jwtTokenProvider;


    @Override
    public LoginResponse login(LoginRequest request) {
        User user = userRepository.findByUsername(request.getUsername())
                .orElseThrow(() -> new EntityNotFoundException("用户不存在"));
        if (!request.getPassword().equals(user.getPassword())) {
            throw new BadCredentialsException("密码错误");
        }

        // 检查是否是商家登录请求
        if (Boolean.TRUE.equals(request.getIsMerchantLogin())) {
            // 如果请求是商家登录，但用户不是商家
            if (!Boolean.TRUE.equals(user.getIsMerchant())) {
                throw new BadCredentialsException("非商家账号，无法进行商家登录");
            }
        }

//        String token = jwtTokenProvider.createToken(user);

        // 返回带有商家信息的登录响应
//        if (Boolean.TRUE.equals(user.getIsMerchant())) {
//            return new LoginResponse(token, user.getUsername(),
//                    true, user.getMerchantId(), user.getMerchantInfo());
//        } else {
//            return new LoginResponse(token, user.getUsername());
//        }
        return null;
    }

    @Override
    public User register(User user) {
        return null;
    }

    @Override
    public User getUserById(Long id) {
        return null;
    }

    @Override
    public User getUserByCustomerId(String customerId) {
        return null;
    }

    @Override
    public User getUserByMerchantId(String merchantId) {
        return null;
    }

    @Override
    public List<User> getAllMerchants() {
        return null;
    }
}
